<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="策略名称" prop="strategyName">
                <el-input v-model="form.strategyName" placeholder="请输入策略名称"/>
            </el-form-item>
            <el-form-item label="告警等级" prop="alarmLevel">
                <el-select v-model="form.alarmLevel">
                    <el-option
                            v-for="dict in alarmLevelOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="警报消息" prop="alarmMessage">
                <el-input v-model="form.alarmMessage" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label="通知方式" prop="notifyMethod">
                <el-select v-model="form.notifyMethod" placeholder="请选择通知方式" clearable>
                    <el-option v-for="item in notifyMethodOptions"
                               :key="item.dictValue"
                               :label="item.dictLabel"
                               :value="item.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="通知人" prop="notifyUserIds">
                <el-select v-model="form.notifyUserIds" filterable placeholder="请选择通知人" multiple
                           :filter-method="filterNotifyUser">
                    <el-option v-for="item in notifyUserList"
                               :key="item.userId"
                               :label="item.nickName"
                               :value="item.userId"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {getUserList} from '/src/api/system/user'
    import {updateStrategy, addStrategy} from '/src/api/alarm/strategy'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            notifyMethodOptions: Array,
            alarmLevelOptions: Array,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    strategyName: [
                        {required: true, message: "策略名称不能为空", trigger: "blur"}
                    ],
                    alarmLevel: [
                        {required: true, message: "告警等级不能为空", trigger: "blur"}
                    ],
                    alarmMessage: [
                        {required: true, message: "警报消息不能为空", trigger: "blur"}
                    ],
                    notifyMethod: [
                        {required: true, message: "通知方式不能为空", trigger: "blur"}
                    ],
                    notifyUserIds: [
                        {required: true, message: "通知人不能为空", trigger: "blur"}
                    ],
                },
                queryParams: {
                    userName: undefined
                },
                //要通知的人员列表
                notifyUserList: [],
            }
        },
        created() {
            this.getUserListByParam()
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.strategyId !== undefined) {
                            updateStrategy(this.form).then(response => {
                                this.msgSuccess('修改成功')
                                this.close()
                            })
                        } else {
                            addStrategy(this.form).then(response => {
                                this.msgSuccess('新增成功')
                                this.close()
                            })
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
            //获取用户列表
            getUserListByParam() {
                getUserList(this.queryParams).then((res) => {
                    this.notifyUserList = res.rows
                })
            },
            filterNotifyUser(userName) {
                this.queryParams.userName = userName
                this.getUserListByParam()
            },
        }
    }
</script>

<style scoped>

</style>
